package com.example.testemail.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 


@RestController
@RequestMapping("email")
public class EmailController {
 
    @Autowired
    private JavaMailSender javaMailSender;
 
    //读取yml文件中username的值并赋值给form
    @Value("${spring.mail.username}")
    private String from;
 
    @RequestMapping("sendEmail")
    public String sendSimpleMail(String email, String subject, String content) {
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件发送者
        message.setFrom(from);
        // 设置邮件接收者
        message.setTo(email);
        // 设置邮件的主题
        message.setSubject(subject);
        // 设置邮件的正文
        //code自行设置，不做实现
//        String code="894393";
//        String text = "您的验证码为：" + code + ",请勿泄露给他人。";
        String text = content;
        message.setText(text);
        // 发送邮件
        try {
            javaMailSender.send(message);
            return "发送成功";
        } catch (MailException e) {
            e.printStackTrace();
        }
        return "发送失败";
    }
}